package com.ykdl.member.kid.marketcard;

import java.util.ArrayList;
import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyPagerAdapter;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.beans.TicketListBean;
import com.ykdl.member.kid.fragments.MyCouponOnLineHasUsedFragment;
import com.ykdl.member.kid.fragments.MyCouponOnLineNotUsedFragment;
import com.ykdl.member.kid.fragments.MyCouponOnLineOutOfDateFragment;
import com.ykdl.member.kid.gears.BaseFragmentActivity;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

/********************************************************************************
 * 
 ** 作者： jiafeiyu 创始时间：2015-7-30 描述： 我的在线优惠券详情列表
 *********************************************************************************/
public class MyCouponOnLineActivity extends BaseFragmentActivity implements
		OnClickListener {
	private int bmpW;
	private int currIndex = 0;
	private int offset = 0;
	private ImageView imageView;
	private ViewPager mTVPager;
	private List<Fragment> mFragList;

	private RelativeLayout can_use, has_used, cannot_use;
	private TextView mTVNotUsed;
	private TextView mTVHasUsed;
	private TextView mTVOutOfDate;
	private int coupon_type;
	private final int UNUSED_TICKETS = 1;// 优惠卷可用列表
	private final int USED_TICKETS_LIST = 2;// 优惠卷已用列表
	private final int EXPIRED_TICKETS_LIST = 3;// 优惠卷过期列表

	private ArrayList<TicketBean> tickets;
	private Fragment mNotUsed;
	private Fragment mHasUsed;
	private Fragment mOutOfDate;
	private int mChoose;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.mycouponactivity);

		initViews();
	}

	private void initViews() {
		// TODO Auto-generated method stub
		InitImageView();
		findViewById(R.id.left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mChoose = getIntent().getIntExtra("choose", 0);
		
		
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("在线支付");
		
		can_use = (RelativeLayout) findViewById(R.id.can_use);
		has_used = (RelativeLayout) findViewById(R.id.has_used);
		cannot_use = (RelativeLayout) findViewById(R.id.cannot_use);

		mTVNotUsed = (TextView) findViewById(R.id.tv_can_use);
		mTVHasUsed = (TextView) findViewById(R.id.tv_has_used);
		mTVOutOfDate = (TextView) findViewById(R.id.tv_cannot_use);

		can_use.setOnClickListener(this);
		has_used.setOnClickListener(this);
		cannot_use.setOnClickListener(this);

		mTVPager = (ViewPager) findViewById(R.id.viewpager);
		initFragment();
		mTVPager.setOnPageChangeListener(new OnPageChangeListener() {
			int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
			int two = one * 2;// 页卡1 -> 页卡3 偏移量

			@Override
			public void onPageSelected(int index) {

				Animation animation = null;
				switch (index) {
				case 0:

					mTVNotUsed.setTextColor(getResources().getColor(
							R.color.mycoupon_red));
					mTVHasUsed.setTextColor(getResources().getColor(
							R.color.mycoupon_gray));
					mTVOutOfDate.setTextColor(getResources().getColor(
							R.color.mycoupon_gray));

					if (currIndex == 1) {
						animation = new TranslateAnimation(one, 0, 0, 0);
					} else if (currIndex == 2) {
						animation = new TranslateAnimation(two, 0, 0, 0);
					}

					break;
				case 1:

					mTVNotUsed.setTextColor(getResources().getColor(
							R.color.mycoupon_gray));
					mTVHasUsed.setTextColor(getResources().getColor(
							R.color.mycoupon_red));
					mTVOutOfDate.setTextColor(getResources().getColor(
							R.color.mycoupon_gray));
					if (currIndex == 0) {
						animation = new TranslateAnimation(offset, one, 0, 0);
					} else if (currIndex == 2) {
						animation = new TranslateAnimation(two, one, 0, 0);
					}
					break;

				default:
					mTVNotUsed.setTextColor(getResources().getColor(
							R.color.mycoupon_gray));
					mTVHasUsed.setTextColor(getResources().getColor(
							R.color.mycoupon_gray));
					mTVOutOfDate.setTextColor(getResources().getColor(
							R.color.mycoupon_red));
					if (currIndex == 0) {
						animation = new TranslateAnimation(offset, two, 0, 0);
					} else if (currIndex == 1) {
						animation = new TranslateAnimation(one, two, 0, 0);
					}
					break;
				}
				// Animation animation = new TranslateAnimation(one * currIndex,
				// one * index, 0, 0);
				currIndex = index;
				animation.setFillAfter(true);
				animation.setDuration(300);
				imageView.startAnimation(animation);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		mTVPager.setCurrentItem(mChoose);
	}

	private void initFragment() {
		mFragList = new ArrayList<Fragment>();
		mNotUsed = new MyCouponOnLineNotUsedFragment();
		mHasUsed = new MyCouponOnLineHasUsedFragment();
		mOutOfDate = new MyCouponOnLineOutOfDateFragment();

		mFragList.add(mNotUsed);
		mFragList.add(mHasUsed);
		mFragList.add(mOutOfDate);

		MyPagerAdapter adapter = new MyPagerAdapter(
				getSupportFragmentManager(), mFragList);
		mTVPager.setAdapter(adapter);
		mTVPager.setOffscreenPageLimit(2);
		
	}
	//初始化指示器位置
		public void InitImageView() {
			// 初始化动画
			imageView = (ImageView) findViewById(R.id.iv_maps_viewpager);
			bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.mycoupon_red_line)
					.getWidth();// 获取图片宽度

			DisplayMetrics dm = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(dm);
			int screenW = dm.widthPixels;// 获取分辨率宽度
			offset = (screenW / 3 - bmpW) / 2;// 计算偏移量

			Matrix matrix = new Matrix();
			matrix.postTranslate(offset, 0);
			imageView.setImageMatrix(matrix);// 设置动画初始位置
		}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.can_use:
			mTVPager.setCurrentItem(0);
			break;
		case R.id.has_used:
			mTVPager.setCurrentItem(1);
			break;
		case R.id.cannot_use:
			mTVPager.setCurrentItem(2);
			break;

		default:
			break;
		}
	}

	// 获取网络数据
	public void getData(int coupon_type) {
		showProgress(MyCouponOnLineActivity.this);
		String url = "";
		switch (coupon_type) {
		case UNUSED_TICKETS:
			url = KidConfig.UNUSED_TICKETS_LIST;
			break;
		case USED_TICKETS_LIST:
			url = KidConfig.USED_TICKETS_LIST;
			break;
		case EXPIRED_TICKETS_LIST:
			url = KidConfig.EXPIRED_TICKETS_LIST;
			break;

		default:
			break;
		}
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, null);
		TaskManager.startHttpRequest(request, new getDataTag(),
				TicketListBean.class);
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			dismissProgress();
			if (t instanceof TicketListBean) {
				TicketListBean bean = (TicketListBean) t;
				if (bean.getError() == null) {
					tickets = bean.getTickets();
				}
			}

		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			dismissProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub

		}

	}

}
